package com.jaya.budan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.CountDownTimer;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.Utils;
import com.jaya.budan.utils.VolleyUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class Exchange_Phone2_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back, rl_forget_password_mobile;
    private String access_token;
    private EditText et_forget_mobile, et_forget_yzm;
    private Button btn_exchange_phone, btn_forget_getSMS;
    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            btn_forget_getSMS.setText(millisUntilFinished / 1000 + "秒后重试");
            btn_forget_getSMS.setEnabled(false);
        }

        @Override
        public void onFinish() {
            btn_forget_getSMS.setText("获取验证码");
            btn_forget_getSMS.setEnabled(true);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_phone2);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        back = findViewById(R.id.back);
        rl_forget_password_mobile = findViewById(R.id.rl_forget_password_mobile);
        et_forget_mobile = findViewById(R.id.et_forget_mobile);
        et_forget_yzm = findViewById(R.id.et_forget_yzm);
        btn_forget_getSMS = findViewById(R.id.btn_forget_getSMS);
        btn_exchange_phone = findViewById(R.id.btn_exchange_phone);
        back.setOnClickListener(this);
        rl_forget_password_mobile.setOnClickListener(this);
        btn_forget_getSMS.setOnClickListener(this);
        btn_exchange_phone.setOnClickListener(this);
        et_forget_mobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11){
                    btn_exchange_phone.setEnabled(true);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_forget_password_mobile:
                ToolUtils.showSoftInputFromWindow(et_forget_mobile);
                break;
            case R.id.btn_forget_getSMS:
                JSONObject object = new JSONObject();
                try {
                    object.put("username", et_forget_mobile.getText().toString().trim());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object);
                break;
            case R.id.btn_exchange_phone:
                postLogOutData();
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.register_GetSMS_Url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("sms", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        timer.start();//启用倒计时
                        ToolUtils.showToast(Exchange_Phone2_Activity.this, message);
                    } else {
                        ToolUtils.showToast(Exchange_Phone2_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Exchange_Phone2_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    /**
     * 网络请求
     */
    private void postLogOutData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.out_login_Url + access_token, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("register", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Utils.logOut(Exchange_Phone2_Activity.this);
                        Intent intent = new Intent(Exchange_Phone2_Activity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
//                        System.exit(0);
                    } else {
                        ToolUtils.showToast(Exchange_Phone2_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Exchange_Phone2_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
